﻿using System;
using System.Collections.Generic;
using System.Net.Mail;
using System.Web;
using System.Text;
using System.Configuration;
using System.Net.Mime;
using System.IO;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI.WebControls;
using System.Web.UI;
namespace BAL
{
    public class Common
    {
        public static string con = BAL.conn.connectionString;
        public static void SendEmail(string From_Address, string To_Address, string Subject, string Content)
        {
            try
            {
                if (To_Address.Trim().Length == 0) { To_Address = ConfigurationManager.AppSettings["ToEmailID"].ToString(); }
                MailMessage mail = new MailMessage();
                mail.From = new MailAddress(ConfigurationManager.AppSettings["FromEmailID"].ToString());

                mail.Subject = Subject;
                string content = Content;
                SmtpClient client = new SmtpClient();
                client.Credentials = new System.Net.NetworkCredential(ConfigurationManager.AppSettings["FromEmailID"].ToString(), ConfigurationManager.AppSettings["EmailPassword"].ToString());
                client.Port = 25;
                string p = HttpContext.Current.Request.UserHostAddress;
                client.Host = ConfigurationManager.AppSettings["EmailHost"].ToString();
                client.DeliveryMethod = SmtpDeliveryMethod.Network;
               // client.EnableSsl = true; //for Gmail
                AlternateView av1 = AlternateView.CreateAlternateViewFromString(content, null, MediaTypeNames.Text.Html);

                LinkedResource logo = new LinkedResource(HttpContext.Current.Server.MapPath("~/Images/Mainlogo.jpg"));

                logo.ContentId = "companylogo";
                
               
                logo.TransferEncoding = System.Net.Mime.TransferEncoding.Base64;

                LinkedResource fb = new LinkedResource(HttpContext.Current.Server.MapPath("~/Images/fb.png"));
                fb.ContentId = "fb";
                fb.TransferEncoding = System.Net.Mime.TransferEncoding.Base64;

                LinkedResource tweet = new LinkedResource(HttpContext.Current.Server.MapPath("~/Images/tw.png"));
                tweet.ContentId = "tweet";
                tweet.TransferEncoding = System.Net.Mime.TransferEncoding.Base64;

                av1.LinkedResources.Add(fb);
                av1.LinkedResources.Add(tweet);
                av1.LinkedResources.Add(logo);
                mail.AlternateViews.Add(av1);
                mail.IsBodyHtml = true;
                mail.To.Add(To_Address);
                client.Send(mail);
            }
            catch (Exception exp)
            {
                throw exp;
            }
        }
        public static void SendEmailToAdmin(string From_Address, string To_Address, string Subject, string Content)
        {
            try
            {
                if (To_Address.Trim().Length == 0) { To_Address = ConfigurationManager.AppSettings["RestaurantOwnerID"].ToString(); }
                MailMessage mail = new MailMessage();
                //mail.From = new MailAddress("host2unlimited@yahoo.com");
                mail.From = new MailAddress(ConfigurationManager.AppSettings["FromEmailID"].ToString());
                mail.CC.Add(new MailAddress(ConfigurationManager.AppSettings["AdminID"].ToString()));
                mail.CC.Add(new MailAddress(ConfigurationManager.AppSettings["CustomerCareID"].ToString()));
                mail.Subject = Subject;
                string content = Content;
                SmtpClient client = new SmtpClient();
                //client.Credentials = new System.Net.NetworkCredential("host2unlimited@yahoo.com", "123456789");
                client.Credentials = new System.Net.NetworkCredential(ConfigurationManager.AppSettings["FromEmailID"].ToString(), ConfigurationManager.AppSettings["EmailPassword"].ToString());
                client.Port = 25;
                string p = HttpContext.Current.Request.UserHostAddress;
                client.Host = ConfigurationManager.AppSettings["EmailHost"].ToString();//"smtp.mail.yahoo.com";//ConfigurationManager.AppSettings["EmailHost"].ToString();
                client.DeliveryMethod = SmtpDeliveryMethod.Network;
                AlternateView av1 = AlternateView.CreateAlternateViewFromString(content, null, MediaTypeNames.Text.Html);

                LinkedResource logo = new LinkedResource(HttpContext.Current.Server.MapPath("~/Images/logo.jpg"));

                logo.ContentId = "companylogo";
                logo.TransferEncoding = System.Net.Mime.TransferEncoding.Base64;

                LinkedResource fb = new LinkedResource(HttpContext.Current.Server.MapPath("~/Images/fb.png"));
                fb.ContentId = "fb";
                fb.TransferEncoding = System.Net.Mime.TransferEncoding.Base64;

                LinkedResource tweet = new LinkedResource(HttpContext.Current.Server.MapPath("~/Images/tw.png"));
                tweet.ContentId = "tweet";
                tweet.TransferEncoding = System.Net.Mime.TransferEncoding.Base64;

                av1.LinkedResources.Add(fb);
                av1.LinkedResources.Add(tweet);
                av1.LinkedResources.Add(logo);

                mail.AlternateViews.Add(av1);
                mail.IsBodyHtml = true;
                mail.To.Add(To_Address);
                client.Send(mail);
            }
            catch (Exception exp)
            {
                throw exp;
            }
        }
        public static void SendMailWithAttachment(string From_Address, string To_Address, string Subject, string Content, string File,bool isCC)
        {
            try
            {
                if (To_Address.Trim().Length == 0) { To_Address = ConfigurationManager.AppSettings["ToEmailID"].ToString(); }
                MailMessage mail = new MailMessage();
                //mail.From = new MailAddress("host2unlimited@yahoo.com");
                mail.From = new MailAddress(ConfigurationManager.AppSettings["FromEmailID"].ToString());
                if (isCC)
                {
                    mail.CC.Add(new MailAddress(ConfigurationManager.AppSettings["AdminID"].ToString()));
                    mail.CC.Add(new MailAddress(ConfigurationManager.AppSettings["CustomerCareID"].ToString()));
                }

                mail.Subject = Subject;
                mail.Attachments.Add(new Attachment(HttpContext.Current.Server.MapPath("~/Invoices/") + File));
                string content = Content;
                SmtpClient client = new SmtpClient();

                client.Credentials = new System.Net.NetworkCredential(ConfigurationManager.AppSettings["FromEmailID"].ToString(), ConfigurationManager.AppSettings["EmailPassword"].ToString());
                client.Port = 25;
                string p = HttpContext.Current.Request.UserHostAddress;
                client.Host = ConfigurationManager.AppSettings["EmailHost"].ToString();//"smtp.mail.yahoo.com";//ConfigurationManager.AppSettings["EmailHost"].ToString();
                client.DeliveryMethod = SmtpDeliveryMethod.Network;
                AlternateView av1 = AlternateView.CreateAlternateViewFromString(content, null, MediaTypeNames.Text.Html);

                LinkedResource logo = new LinkedResource(HttpContext.Current.Server.MapPath("~/Images/logo.jpg"));

                logo.ContentId = "companylogo";
                logo.TransferEncoding = System.Net.Mime.TransferEncoding.Base64;

                LinkedResource fb = new LinkedResource(HttpContext.Current.Server.MapPath("~/Images/fb.png"));
                fb.ContentId = "fb";
                fb.TransferEncoding = System.Net.Mime.TransferEncoding.Base64;

                LinkedResource tweet = new LinkedResource(HttpContext.Current.Server.MapPath("~/Images/tw.png"));
                tweet.ContentId = "tweet";
                tweet.TransferEncoding = System.Net.Mime.TransferEncoding.Base64;

                av1.LinkedResources.Add(fb);
                av1.LinkedResources.Add(tweet);
                av1.LinkedResources.Add(logo);

                mail.AlternateViews.Add(av1);
                mail.IsBodyHtml = true;
                mail.To.Add(To_Address);
                client.Send(mail);
            }
            catch (Exception exp)
            {
                throw exp;
            }
        }
        public static void logError(String sHeader, String sError)
        {
            String sPath;
            String sActualError;
            StreamWriter errWriter;
            try
            {
                //' Get Actual Path of "Error" stored in Web.config
                sPath = System.Web.HttpContext.Current.Request.PhysicalApplicationPath + ConfigurationManager.AppSettings["sPathErrorLog"];
                //sPath = System.Web.HttpContext.Current.Request.PhysicalApplicationPath + "ErrorLog\\";

                //' Generates Path & LogFile Name of  ErrorLog
                sPath = sPath + DateTime.Now.Day + "-" + DateTime.Now.Month + "-" + DateTime.Now.Year + "-"+ DateTime.Now.Ticks +".log";

                //' Generates Error Message
                sActualError = System.DateTime.Now + " : " + sHeader + " : " + sError;

                //' Creation of File.
                errWriter = new StreamWriter(sPath, true, System.Text.Encoding.ASCII);

                errWriter.WriteLine("");
                errWriter.WriteLine(sActualError);
                errWriter.Close();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
            }
        }
        public static string GetCityName(int idcity)
        {
            SqlParameter[] arrParam = new SqlParameter[1];
            arrParam[0] = new SqlParameter("@IdCity", SqlDbType.Int);
            arrParam[0].Value = idcity;

            string Cityname = VocationSpace.DAL.SqlHelper.ExecuteScalar(con, CommandType.StoredProcedure, "CM_GetCityName", arrParam).ToString();
            return Cityname;

        }
        public static DataSet GetAllCities()
        {
            DataSet ds = VocationSpace.DAL.SqlHelper.ExecuteDataset(con, "CM_GetCities");
            return ds;
        }
        public static int GetCityID(string cityname)
        {
            SqlParameter[] arrParam = new SqlParameter[1];
            arrParam[0] = new SqlParameter("@Cityname", SqlDbType.VarChar);
            arrParam[0].Value = cityname;

            int cityId = Convert.ToInt32(VocationSpace.DAL.SqlHelper.ExecuteScalar(con, CommandType.StoredProcedure, "CM_GetCityIDByMName", arrParam));
            return cityId;

        }
        public static void PopulateContinent(ref DropDownList ddlContinent)
        {
            DataSet ds = VocationSpace.DAL.SqlHelper.ExecuteDataset(con, CommandType.StoredProcedure, "VS_GetContinenetList");
            if (ds.Tables.Count > 0) {
                if (ds.Tables[0].Rows.Count > 0)
                {
                    foreach (DataRow dr in ds.Tables[0].Rows)
                    {
                        ddlContinent.DataSource = ds.Tables[0];
                        ddlContinent.DataValueField = "IDContinent";
                        ddlContinent.DataTextField = "Continent";
                        ddlContinent.DataBind();
                        foreach (ListItem _listItem in ddlContinent.Items)
                        {

                            _listItem.Attributes.Add("title", _listItem.Text + _listItem.Value);

                        }
                        
                    }
                }
            }


        }
        public static void PopulateCountry(ref DropDownList ddlCountry, int IdContinent)
        {
            SqlParameter[] arrParam = new SqlParameter[1];

            arrParam[0] = new SqlParameter("@IdContinent", SqlDbType.Int);
            arrParam[0].Value = IdContinent;

            DataSet ds = VocationSpace.DAL.SqlHelper.ExecuteDataset(con, CommandType.StoredProcedure, "VS_GetAllCountriesByContinent", arrParam);
            if (ds.Tables.Count > 0)
            {
                if (ds.Tables[0].Rows.Count > 0)
                {
                    ddlCountry.DataSource = ds.Tables[0];
                    ddlCountry.DataTextField = "CountryName";
                    ddlCountry.DataValueField = "IDCountry";
                    ddlCountry.DataBind();
                }
            }
        }
    }
}

